//
//  FQSettingViewController.swift
//  SwiftProject
//
//  Created by 董駸 on 2022/8/19.
//

import UIKit

class FQSettingViewController: BaseViewController, UITableViewDataSource, UITableViewDelegate {

    let mainTable = UITableView(frame: .zero, style: .grouped)
    let dataArray : [String] = ["用户协议",
                                "关于我们",
                                "意见反馈",
                                "注销账号"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationBarType = .light
    }
    
    override func initSubviews() {
        super.initSubviews()
        
        mainTable.dataSource = self
        mainTable.delegate = self
        mainTable.register(BaseTableViewCell.self, forCellReuseIdentifier: "cell")
        mainTable.backgroundColor = .qmui_color(withHexString: "f5f5f5")
        mainTable.rowHeight = 50
        view.addSubview(mainTable)
        
        mainTable.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(NavigationContentTop)
//            make.height.equalTo(dataArray.count * 50 + 16 * 2 + 50)
        }
        
        let logoutBtn = QMUIButton(title: "安全退出", titleColor: .color51, font: .rSFont(16))
        logoutBtn.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 50)
        logoutBtn.addTarget(self, action: #selector(logout), for: .touchUpInside)
        logoutBtn.backgroundColor = .white
        mainTable.tableFooterView = logoutBtn
    
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        cell?.textLabel?.text = dataArray[indexPath.row]
        cell?.accessoryType = .disclosureIndicator
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        switch indexPath.row {
        case 0:
            navigationController?.qmui_pushViewController(FQPrivacyListViewController(), animated: true)
        case 1:
            navigationController?.qmui_pushViewController(FQAboutUsViewController(), animated: true)
        case 3:
            navigationController?.qmui_pushViewController(FQLogoutViewController(), animated: true)
        default:
            navigationController?.qmui_pushViewController(FQFeedbackViewController(), animated: true)
        }
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        cell.separatorInset = .zero
    }
    
    @objc func logout() {
        BaseUserModel.logout()
        navigationController?.qmui_popToRootViewController(animated: false, completion: {
            AppDelegate.appDelegate.window?.rootViewController = QMUINavigationController(rootViewController: LoginViewController())
        })
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        16
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        nil
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        16
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        nil
    }

    override func setupNavigationItems() {
        super.setupNavigationItems()
        title = "设置"
    }

}
